<?php
namespace app\index\controller;
use app\index\model\Project;
use app\index\model\Detail;
use think\Controller;
use app\admin\controller\Program;
use think\Db;

class Makescore extends Base
{
    /**
     * 首页展示正在进行的比赛
     * @return \think\response\View
     */
    public function matching()
    {
        $pro = new Project();
        $list = $pro->where('status',2)->find();
        $this->assign('list',$list);
		$this->assign('puship',config('puship'));
        return view();
    }

    /**
     * 添加评委对当前场次评分
     */
    public function addScore()
    {
		$pro = new Project();
		$run = $pro->where('status',2)->find();
		if(!$run){
			return ['status'=>3];
		}
        $data = input();

        $det = new Detail();
        if($data['id'] == 0) {
            return ['status'=>0];
        }
        //判断当前评委对当前场次是否已评论
        $list = $det->where('pid',$data['id'])->where('openid',$this->pfopenid)->find();
        if($list) {
            return ['status'=>2];
        }

        $total = $data['music'] + $data['skill'] + $data['fluency'] + $data['voice'] + $data['attach'];
        $tmp['pid'] = $data['id'];
        $tmp['openid'] = $this->pfopenid;
        $tmp['learn'] = $data['music'];
        $tmp['skill'] = $data['skill'];
        $tmp['fluency'] = $data['fluency'];
        $tmp['control'] = $data['voice'];
        $tmp['influence'] = $data['attach'];
        $tmp['totalscore'] = $total;
        $rel = $det->insert($tmp);

        //在此调用后台接口，计算平均分，推送给后台
        $pro = new Program();
        $score = $pro->avg($data['id']);
        $pro->send($score);
        if($rel) {
            return ['status'=>1];
        }else {
            return ['status'=>0];
        }
    }
}